import React from 'react';
import {injectIntl} from './intl';
import PropTypes from 'prop-types';
import {Button, Spin} from 'antd';
import {hasPermissions} from './utils';
import {PERMISSION} from './constants';
import {PAGE} from '../constants';
import routes from './routes';
import WrappedPopconfirm from '../common/WrappedPopconfirm';
import styles from './style.css';
import CommonDetail from './CommonDetail';
import SocSelectPanel from './SocSelectPanel';
export class Detail extends React.PureComponent {
    state = {
        loading: false,
        socSelectPanelShow: false
    };

    componentDidMount() {
        this.props.init();
        this.ismounted = true;
    }

    componentWillUnmount() {
        this.ismounted = false;
        this.props.onUmount();
    }

    handleProjectClick = () => {
        this.props.history.push({
            pathname: routes.project.url(),
            query: {
                dealerId: this.props.detail.dealerId,
                brandId: this.props.detail.brandId,
                partId: this.props.detail.partId,
                productCategoryId: this.props.detail.productCategoryId,
                lv1FaultItemCode: this.props.detail.lv1FaultItemCode,
                lv2FaultItemCode: this.props.detail.lv2FaultItemCode,
                lv3FaultItemCode: this.props.detail.lv3FaultItemCode,
            }
        });
    }
    handleCloseClick = () => {
        this.setState({
            loading: true
        });
        this.props.onClickClose({
            dealerId: this.props.detail.dealerId,
            brandId: this.props.detail.brandId,
            partId: this.props.detail.partId,
            productCategoryId: this.props.detail.productCategoryId,
            lv1FaultItemCode: this.props.detail.lv1FaultItemCode,
            lv2FaultItemCode: this.props.detail.lv2FaultItemCode,
            lv3FaultItemCode: this.props.detail.lv3FaultItemCode,
        }).then(success => {
            if(this.ismounted && !success)
                this.setState({
                    loading: false
                });
        });
    };
    onSocSelect = r => {
        const {detail} = this.props;
        const condition = {
            socId: r.id,
            dealerId: detail.dealerId,
            brandId: detail.brandId,
            partId: detail.partId,
            productCategoryId: detail.productCategoryId,
            lv1FaultItemCode: detail.lv1FaultItemCode,
            lv2FaultItemCode: detail.lv2FaultItemCode,
            lv3FaultItemCode: detail.lv3FaultItemCode
        };
        this.props.onSubmit(condition).then(success => {
            if(this.ismounted && !success)
                this.setState({
                    socSelectPanelShow: false
                });
        });
    }
    onSocCancel = () => {
        this.setState({
            socSelectPanelShow: false
        });
    }
    onClickJoin = () => {
        this.setState({
            socSelectPanelShow: true
        });
    }
    render() {
        const {options = []} = this.props.detail;
        const operations = [];
        if(hasPermissions(this.props.permissions, PERMISSION.project) && hasPermissions(options, PERMISSION.project))
            operations.push(
                <Button key="project" onClick={this.handleProjectClick} size="small">
                    {this.props.intl.formatMessage({
                        id: 'detail.btn.project',
                        defaultMessage: '立项'
                    })}
                </Button>
            );
        if(hasPermissions(this.props.permissions, PERMISSION.join) && hasPermissions(options, PERMISSION.join))
            operations.push(
                <Button key="join" onClick={this.onClickJoin} size="small">
                    {this.props.intl.formatMessage({
                        id: 'detail.btn.join',
                        defaultMessage: '加入项目'
                    })}
                </Button>
            );
        if(hasPermissions(this.props.permissions, PERMISSION.close) && hasPermissions(options, PERMISSION.close))
            operations.push(
                <WrappedPopconfirm
                    id="close"
                    key="close"
                    title={this.props.intl.formatMessage({
                        id: 'detail.popconfirm.message',
                        defaultMessage: '是否继续?'
                    })}
                    onConfirm={this.handleCloseClick}>
                    <Button size="small">
                        {this.props.intl.formatMessage({
                            id: 'detail.btn.close',
                            defaultMessage: '关闭'
                        })}
                    </Button>
                </WrappedPopconfirm>
            );
        return (
            <div className={styles.detail}>
                <Spin spinning={this.props.loading || this.state.loading}>
                    <CommonDetail
                        loading={this.props.loading}
                        detail={this.props.detail}
                        operations={operations}/>
                </Spin>
                <div className="page-toolbar">
                    <Button type="primary" disabled={this.props.loading} onClick={this.props.onCancel}>
                        {this.props.intl.formatMessage({
                            id: 'detail.btn.retrun',
                            defaultMessage: '返回'
                        })}
                    </Button>
                </div>
                {
                    this.state.socSelectPanelShow &&
                    <SocSelectPanel
                        brandCode={this.props.detail.brandCode}
                        brandId={this.props.detail.brandId}
                        dealerCode={this.props.detail.dealerCode}
                        dealerId={this.props.detail.dealerId}
                        lv1FaultItemCode={this.props.detail.lv1FaultItemCode}
                        lv2FaultItemCode={this.props.detail.lv2FaultItemCode}
                        lv3FaultItemCode={this.props.detail.lv3FaultItemCode}
                        partCode={this.props.detail.partCode}
                        productCategoryCode={this.props.detail.productCategoryCode}
                        onCancel={this.onSocCancel}
                        onSelect={this.onSocSelect}/>
                }
            </div>
        );
    }
}
Detail.propTypes = {
    detail: PropTypes.object.isRequired,
    init: PropTypes.func.isRequired,
    loading: PropTypes.bool.isRequired,
    permissions: PropTypes.array.isRequired,
    query: PropTypes.object.isRequired,
    onCancel: PropTypes.func.isRequired,
    onClickClose: PropTypes.func.isRequired,
    onSubmit: PropTypes.func.isRequired,
    onUmount: PropTypes.func.isRequired,
    history: PropTypes.object,
    intl: PropTypes.object
};
import {connect} from 'react-redux';
import {
    getDetail,
    clearData,
    tableSearch,
    onClickClose,
    onClickJoin
} from './actions';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
const getPermissions = selectorFactory(['page', 'domainData', 'permission', 'data']);
const getDetailData = selectorFactory(['page', 'domainData', 'detail', 'data']);

const mapStateToProps = (state, ownProps) => ({
    loading: state.getIn(['page', 'domainData', 'detail', 'isFetching']) || state.getIn(['page', 'domainData', 'initData', 'isFetching']),
    detail: getDetailData(state),
    permissions: getPermissions(state),
});

const mapDispatchToProps = (dispatch, ownProps) => ({
    init: () => dispatch(getDetail(ownProps.query)),
    onCancel: () => {
        ownProps.history.push(routes.query.url());
    },
    onClickClose: data =>
        dispatch(onClickClose(data)).then(success => {
            if(success) {
                dispatch(tableSearch({
                    pageIndex: PAGE.index
                }));
                ownProps.history.push(routes.query.url());
            }
            return success;
        }),
    onUmount: () => dispatch(clearData()),
    onSubmit: condition =>
        dispatch(onClickJoin(condition)).then(res => {
            if(res.ok) {
                dispatch(tableSearch({
                    pageIndex: PAGE.index
                }));
                ownProps.history.push(routes.query.url());
            }
            return res.ok;
        }),
});

export default connect(
    mapStateToProps,
    mapDispatchToProps
)(injectIntl(Detail));
